library(tidyverse)
library(readxl)
library(naniar)
library(stringr)

setwd("./data/raw")

dat <- read_xlsx("shareredszips.xlsx")

dat <- dat %>%
  mutate(share_zips_obs = num_zips_obs/(num_zips_obs + num_zips_sup)) %>%
  replace_with_na(replace = list(share_reds = ".")) %>%
  mutate(share_reds = as.numeric(share_reds)) %>%
  select(!c(starts_with("num_")))

dat <- pivot_longer(data = dat,
                    cols = starts_with("share"),
                    names_to = "var",
                    names_prefix = "share_",
                    values_to = "share") %>%
  mutate(var = case_match(var,
                          "reds" ~ "Redemptions observed",
                          "food" ~ "Food costs observed",
                          "zips_obs" ~ "ZIPs with stores with redemptions")) %>%
  mutate(share = share*100) %>%
  group_by(var)

fig <- ggplot(data = dat, aes(x = yr,
                              y = share,
                              color = var,
                              group = var,
                              linetype = var)) +
  geom_line() + 
  labs(x = "Fiscal year",
       y = "%",
       color = "Share of ...",
       linetype = "Share of ...") +
  theme_minimal() +
  theme(legend.position = "bottom") +
  scale_y_continuous(breaks = seq(0,30,5), limits = c(0,32)) +
  scale_x_continuous(breaks = seq(2007,2016,3), limits = c(2005, 2018))

setwd("./analysis/output/graphs")

ggsave("share_reds_zips.png",
       device = "png",
       dpi = 600,
       width = 6.5,
       height = 4,
       units = "in")